﻿#include "BrowserTableHandlerPhysicsGeneralLaminate.h"
#include "OperatorsInterface/OperAbstractPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKPhyscisPropertyGeneralLaminate.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsPhysicsProp.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include <QPushButton>

namespace GUI
{
    void BrowserTableHandlerPhysicsGeneralLaminate::updateTable()
    {
        if (_tableWidget == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

        //获取物理属性管理器
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsPhysicsPropManager* mgr = phys->getPhysicsPropManager();
        if (mgr == nullptr) return;
        _maxlayers = 0;
        QList<Acoustics::FITKAcousticsAbsPhysicsProp*> list = mgr->getPhysicsProps(
            Acoustics::FITKAcousticsAbsPhysicsProp::APPGeneralLaminatePhysicalProperty);
        for (int i = 0; i < list.size(); ++i)
        {
            //转换为自由阻尼板
            Acoustics::FITKPhyscisPropertyGeneralLaminate* prop = dynamic_cast<Acoustics::FITKPhyscisPropertyGeneralLaminate*>(list.at(i));
            if (prop == nullptr) continue;
            //记录最大层数
             int layers = prop->getLayerCount();  
             if (layers > _maxlayers)
                 _maxlayers = layers;
        }
        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Layers");
        for (int i = 0; i < _maxlayers; ++i)
        {
            QString Layer = QString("Layer-%1").arg(i + 1);
            headerLabels << Layer;            
        }
        headerLabels << tr("Total Thickness[m]") << tr("Color") << tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }
    void BrowserTableHandlerPhysicsGeneralLaminate::updateData()
    {
        if (_tableWidget == nullptr) return;
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        //获取物理属性管理器
        Acoustics::FITKAcousticsPhysicsPropManager* mgr = phys->getPhysicsPropManager();
        if (mgr == nullptr) return;

        QList<Acoustics::FITKAcousticsAbsPhysicsProp*> list = mgr->getPhysicsProps(
            Acoustics::FITKAcousticsAbsPhysicsProp::APPGeneralLaminatePhysicalProperty);
        //物理属性数量
        const int n = list.size();
        _tableWidget->setRowCount(n);

        for (int i = 0; i < n; ++i)
        {
            //转换为自由阻尼板
            Acoustics::FITKPhyscisPropertyGeneralLaminate* prop = dynamic_cast<Acoustics::FITKPhyscisPropertyGeneralLaminate*>
                (list.at(i));
            if (prop == nullptr) continue;
            //数据ID
            const int id = prop->getDataObjectID();
            //填充表格，用type表示ID
            _tableWidget->setItem(i, 0, new QTableWidgetItem(prop->getDataObjectName(), id));
            _tableWidget->setItem(i, 1, new QTableWidgetItem(QString::number(prop->getLayerCount()), id));
            //填充每一层的属性
            for (int j = 0; j < prop->getLayerCount(); ++j)
            {
                auto isoLayerData = prop->getDataByIndex(j);
                _tableWidget->setItem(i, 1+j+1, new QTableWidgetItem(isoLayerData->getMaterialName(), id));
            }
            _tableWidget->setItem(i, 1+_maxlayers+1, new QTableWidgetItem(QString::number(prop->getTotalThickness()), id));
            float color[3] = { 0,0,0 };
            prop->getColor(color);
            QPushButton* button = new QPushButton(_tableWidget);
            this->SetPushButtonColor(button, color[0] * 255, color[1] * 255, color[2] * 255);
            _tableWidget->setCellWidget(i, 1 + _maxlayers + 2, button);
            connect(button, &QPushButton::clicked, this, [=] {this->onEditItemColor(button, prop); });
            _tableWidget->setItem(i, 1+_maxlayers+3, new QTableWidgetItem(prop->getDescribe(), id));
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }

    EventOper::OperAbstractPhysics * BrowserTableHandlerPhysicsGeneralLaminate::getPhysicsPropOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractPhysics>("OperPhysicsPropGeneralLaminate");
    }

    bool BrowserTableHandlerPhysicsGeneralLaminate::enableAttriSet()
    {
        //自由阻尼板无批量操作
        return false;
    }
}

